package cn.stylefeng.guns.modular.contactus.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.contactus.entity.ContactUs;
import cn.stylefeng.guns.modular.contactus.mapper.ContactUsMapper;
import cn.stylefeng.guns.modular.contactus.model.params.ContactUsParam;
import cn.stylefeng.guns.modular.contactus.model.result.ContactUsResult;
import  cn.stylefeng.guns.modular.contactus.service.ContactUsService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 联系我们 服务实现类
 * </p>
 *
 * @author liuqiang
 * @since 2020-02-12
 */
@Service
public class ContactUsServiceImpl extends ServiceImpl<ContactUsMapper, ContactUs> implements ContactUsService {

    @Override
    public void add(ContactUsParam param){
        ContactUs entity = getEntity(param);
        ContactUs oldEntity = getOldEntity(param);
        if(oldEntity != null){
            this.updateById(entity);
        }
        this.save(entity);
    }

    @Override
    public void delete(ContactUsParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ContactUsParam param){
        ContactUs oldEntity = getOldEntity(param);
        ContactUs newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ContactUsResult findBySpec(ContactUsParam param){
        return null;
    }

    @Override
    public List<ContactUsResult> findListBySpec(ContactUsParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ContactUsParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ContactUsParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ContactUs getOldEntity(ContactUsParam param) {
        return this.getById(getKey(param));
    }

    private ContactUs getEntity(ContactUsParam param) {
        ContactUs entity = new ContactUs();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
